//package com.example.easyexcel.excelOld.service.impl;
//
//import com.alibaba.excel.EasyExcel;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.example.easyexcel.excelOld.config.CommentsWriteHandler;
//import com.example.easyexcel.excelOld.config.ExcelListener;
//import com.example.easyexcel.excelOld.domain.*;
//import com.example.easyexcel.excelOld.mapper.RecordsMapper;
//import com.example.easyexcel.excelOld.mapper.accountMapper;
//import com.example.easyexcel.excelOld.service.accountService;
//import com.example.easyexcel.excelOld.util.RedisUtil;
//import com.google.gson.Gson;
//import com.google.gson.GsonBuilder;
//import com.google.gson.reflect.TypeToken;
//import lombok.SneakyThrows;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.lang.reflect.Type;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
//@Service
//public class accountServiceImpl extends ServiceImpl<accountMapper, AccountEntity> implements accountService {
//    private static final String PREFIX = "excel_";
//    public static final String DATA_LIST = "data_";
//    public static final String DATA_LIST_ERROR = "_error";
//    public static final long EXPIRE_TIME = 60*10;
//    @Autowired
//    accountMapper accountMapper;
//    @Autowired
//    GsonBuilder gsonBuilder;
//    @Autowired
//    RedisUtil redisUtil;
//    @Autowired
//    RecordsMapper recordsMapper;
//    @Override
//    public int batchInsert(List<AccountEntity> list) {
//        boolean b = this.saveBatch(list);
//        return 1;
//    }
//
//    @Override
//    public void importExcel(MultipartFile file) throws IOException {
//        ExcelListener excelListener = new ExcelListener();
//        //获取文件名
//        String fileName = file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf("."));
//        //读取文件
//        EasyExcel.read(file.getInputStream(), ExportVo.class, excelListener).sheet().doRead();
//        //获取文件数据
//        List<ExportVo> listExcels = excelListener.getDataList();
//        //获取错误得数据
//        Map<Integer, List<ExcelError>> excelErrorMap = excelListener.getExcelErrorMap();
//        //每次导入得标志。用于错误excel返回，导入详情日志记录查询
//        String batchId = String.valueOf(UUID.randomUUID());
//
//        //如果错误数据集合有数据，将所有数据和错误数据集合存入redis
//        if (excelErrorMap.size() > 0) {
//            Gson gson = gsonBuilder.create();
//            String key = PREFIX + DATA_LIST + batchId;
//            //excel_data_f27f1b7c-5fff-4eef-9134-4fdf50be00d5
//            redisUtil.set(key, gson.toJson(listExcels), EXPIRE_TIME);
//            redisUtil.set(key + DATA_LIST_ERROR, gson.toJson(excelErrorMap), EXPIRE_TIME);
//            //将错误数据存入数据库
//            ImportRecordEntity entity = new ImportRecordEntity();
//            entity.setBatchId(batchId).setFileName(fileName).setStatus("失败").setDetails( gson.toJson(excelErrorMap));
//            recordsMapper.insert(entity);
//            //返回 batchId 根据batchId可以返回错误得带有批注得excel
//        } else {
//            // 否则批量插入数据库
//
//        }
//    }
//
//    @SneakyThrows
//    @Override
//    public void downloadErrorExcelj(HttpServletResponse response,String uuid) {
//        downloadEr(response,uuid,ExportVo.class);
//
////        SimpleDateFormat fDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
////        String fileName = fDate.format(new Date()) + ".xlsx";
////        response.setContentType("application/vnd.ms-excel");
////        response.setCharacterEncoding("utf-8");
////        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
////        CommentsWriteHandler commentWriteHandler = new CommentsWriteHandler();
////        String key = PREFIX + DATA_LIST + uuid;
////        String listExcelJson = (String) redisUtil.get(key);
////        String listExcelErrorlJson = (String) redisUtil.get(key + DATA_LIST_ERROR);
////        Gson gson = gsonBuilder.create();
////        if (listExcelJson != null && listExcelErrorlJson != null) {
////            Type listExcelJsonType = new TypeToken<List<ExportVo>>() {
////            }.getType();
////            List<ExportVo> sendListExcels = gson.fromJson(listExcelJson, listExcelJsonType);
////            Type listExcelErrorlJsonType = new TypeToken<Map<Integer, List<ExcelError>>>() {
////            }.getType();
////            Map<Integer, List<ExcelError>> errorMap = gson.fromJson(listExcelErrorlJson, listExcelErrorlJsonType);
////            commentWriteHandler.setExcelErrorMap(errorMap);
////            EasyExcel.write(response.getOutputStream(), ExportVo.class)
////                    .inMemory(Boolean.TRUE)
////                    .sheet("sheet1")
////                    //注册批注拦截器
////                    .registerWriteHandler(commentWriteHandler)
////                    .doWrite(sendListExcels);
////        }
//    }
//
//    @Override
//    public RecordVo getImportRecord(String id) {
//            Gson gson = gsonBuilder.create();
//            ImportRecordEntity entity = recordsMapper.selectById(id);
//            RecordVo records = new RecordVo();
//            BeanUtils.copyProperties(entity,records);
//            String details = entity.getDetails();
//            Type listExcelErrorlJsonType = new TypeToken<Map<Integer, List<ExcelError>>>() {
//            }.getType();
//            Map<Integer, List<ExcelError>> errorMap = gson.fromJson(details, listExcelErrorlJsonType);
//            records.setImportRecord(errorMap);
//            return records;
//    }
//
//    @Override
//    public List<RecordVo> getRecordList() {
//        List<ImportRecordEntity> importRecordEntities = recordsMapper.selectList(null);
//        List<RecordVo> recordVos = new ArrayList<>();
//        for (ImportRecordEntity importRecordEntity : importRecordEntities) {
//            Gson gson = gsonBuilder.create();
//            RecordVo records = new RecordVo();
//            BeanUtils.copyProperties(importRecordEntity,records);
//            String details = importRecordEntity.getDetails();
//            Type listExcelErrorlJsonType = new TypeToken<Map<Integer, List<ExcelError>>>() {
//            }.getType();
//            Map<Integer, List<ExcelError>> errorMap = gson.fromJson(details, listExcelErrorlJsonType);
//            records.setImportRecord(errorMap);
//            recordVos.add(records);
//        }
//        return recordVos;
//    }
//
//    @Override
//    public List<AccountEntity> getList(AccountDto dto) {
//        String userName = dto.getUserName();
//        LambdaQueryWrapper<AccountEntity> qu = new LambdaQueryWrapper<>();
//        String[] split = userName.split(",");
//        qu.in(AccountEntity::getUserName, Arrays.asList(split)).eq(AccountEntity::getAccount,dto.getAccount());
//        List<AccountEntity> datas = accountMapper.selectList(qu);
//        return datas;
//    }
//
//    public <T> void downloadEr(HttpServletResponse response,String uuid,Class<T> clazz) throws IOException {
//        SimpleDateFormat fDate = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
//        String fileName = fDate.format(new Date()) + ".xlsx";
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
//        CommentsWriteHandler commentWriteHandler = new CommentsWriteHandler();
//        String key = PREFIX + DATA_LIST + uuid;
//        String listExcelJson = (String) redisUtil.get(key);
//        String listExcelErrorlJson = (String) redisUtil.get(key + DATA_LIST_ERROR);
//        Gson gson = gsonBuilder.create();
//        if (listExcelJson != null && listExcelErrorlJson != null) {
//            Type listExcelJsonType = new TypeToken<List<T>>() {}.getType();
//            List<T> sendListExcels = gson.fromJson(listExcelJson, listExcelJsonType);
//            Type listExcelErrorlJsonType = new TypeToken<Map<Integer, List<ExcelError>>>() {
//            }.getType();
//            Map<Integer, List<ExcelError>> errorMap = gson.fromJson(listExcelErrorlJson, listExcelErrorlJsonType);
//            commentWriteHandler.setExcelErrorMap(errorMap);
//            EasyExcel.write(response.getOutputStream(), ExportVo.class)
//                    .inMemory(Boolean.TRUE)
//                    .sheet("sheet1")
//                    //注册批注拦截器
//                    .registerWriteHandler(commentWriteHandler)
//                    .doWrite(sendListExcels);
//        }
//    }
//}
